/**
 * 
 */
package neptune.web.client.controller;

import neptune.web.client.WebInterface;
import neptune.web.client.commands.SaveNodeAsTemplateCommand;
import neptune.web.client.services.ClusterService;
import neptune.web.client.view.ConsoleManager;
import neptune.web.client.view.components.NodesManager;
import neptune.web.client.view.components.NodesManager.NodeCommandHandler;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Gestisce i comandi riguardanti i nodi virtuali appartenenti ad un
 * esperimento.
 * 
 * @author Roberto Bifulco [RobertoBifulco.it] [info@robertoBifulco.it]
 * 
 */
public class ExperimentNodeCommandHandler implements NodeCommandHandler {

	private static final String EXPID_NODEID_SEPARATOR = ":";
	private final String expId;

	public ExperimentNodeCommandHandler(String expId) {
		this.expId = expId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seeneptune.web.client.view.components.NodesManager.NodeCommandHandler#
	 * nodeConsoleCommandFired(java.lang.String)
	 */
	public void nodeConsoleCommandFired(String nodeId, NodesManager sender) {
		/*
		 * String msg="Damn! You discovered a feature not yet implemented! \n" +
		 * "We are working to activate this very useful button. \n" +
		 * "In the meanwhile you can use an external software linke Putty!";
		 * WebInterface.showMessageDialog( new MessageWidget(0,msg),true);
		 */
		Window.open("jcterm.jnlp", "_blank", "");
		// Window.open("sshapplet.html", "_blank","");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seeneptune.web.client.view.components.NodesManager.NodeCommandHandler#
	 * nodePauseCommandFired(java.lang.String)
	 */
	public void nodePauseCommandFired(final String nodeId,
			final NodesManager sender) {
		String message = "Pausing node '" + nodeId + EXPID_NODEID_SEPARATOR
				+ expId + "'...";
		ConsoleManager.addConsoleMessage(message, false);
		sender.setVMIcon(nodeId, NodesManager.UPDATING_ICON);

		ClusterService.Util.getInstance().pauseNode(
				nodeId + EXPID_NODEID_SEPARATOR + expId,
				new AsyncCallback<String>() {

					public void onSuccess(String result) {
						String message = "Node '" + nodeId
								+ EXPID_NODEID_SEPARATOR + expId + "' paused";
						ConsoleManager.addConsoleMessage(message, false);
						sender.setNodeInfos(nodeId, NodesManager.Status.PAUSED);
					}

					public void onFailure(Throwable caught) {
						String message = "Node '" + nodeId
								+ EXPID_NODEID_SEPARATOR + expId
								+ "' pause command exectution error. Cause: \n"
								+ caught.getMessage();
						WebInterface.addMessageToConsoleAndShowDialog(message,
								true);
						sender
								.setNodeInfos(nodeId,
										NodesManager.Status.UNKNOWN);
					}
				});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seeneptune.web.client.view.components.NodesManager.NodeCommandHandler#
	 * nodeRebootCommandFired(java.lang.String)
	 */
	public void nodeRebootCommandFired(final String nodeId,
			final NodesManager sender) {
		String message = "Rebooting node '" + nodeId + EXPID_NODEID_SEPARATOR
				+ expId + "'...";
		ConsoleManager.addConsoleMessage(message, false);
		sender.setVMIcon(nodeId, NodesManager.UPDATING_ICON);

		ClusterService.Util.getInstance().rebootNode(
				nodeId + EXPID_NODEID_SEPARATOR + expId,
				new AsyncCallback<String>() {

					public void onSuccess(String result) {
						String message = "Node '" + nodeId
								+ EXPID_NODEID_SEPARATOR + expId + "' rebooted";
						ConsoleManager.addConsoleMessage(message, false);
						sender
								.setNodeInfos(nodeId,
										NodesManager.Status.RUNNING);
					}

					public void onFailure(Throwable caught) {
						String message = "Node '"
								+ nodeId
								+ "."
								+ expId
								+ "' reboot command exectution error. Cause: \n"
								+ caught.getMessage();
						WebInterface.addMessageToConsoleAndShowDialog(message,
								true);
						sender
								.setNodeInfos(nodeId,
										NodesManager.Status.UNKNOWN);
					}
				});

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seeneptune.web.client.view.components.NodesManager.NodeCommandHandler#
	 * nodeStartCommandFired(java.lang.String)
	 */
	public void nodeStartCommandFired(final String nodeId,
			final NodesManager sender) {
		String message = "Starting node '" + nodeId + EXPID_NODEID_SEPARATOR
				+ expId + "'...";
		ConsoleManager.addConsoleMessage(message, false);
		sender.setVMIcon(nodeId, NodesManager.UPDATING_ICON);

		ClusterService.Util.getInstance().startNode(
				nodeId + EXPID_NODEID_SEPARATOR + expId,
				new AsyncCallback<String>() {

					public void onSuccess(String result) {
						String message = "Node '" + nodeId
								+ EXPID_NODEID_SEPARATOR + expId + "' started";
						ConsoleManager.addConsoleMessage(message, false);
						sender
								.setNodeInfos(nodeId,
										NodesManager.Status.RUNNING);
					}

					public void onFailure(Throwable caught) {
						String message = "Node '" + nodeId
								+ EXPID_NODEID_SEPARATOR + expId
								+ "' start command exectution error. Cause: \n"
								+ caught.getMessage();
						WebInterface.addMessageToConsoleAndShowDialog(message,
								true);
						sender
								.setNodeInfos(nodeId,
										NodesManager.Status.UNKNOWN);
					}
				});

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seeneptune.web.client.view.components.NodesManager.NodeCommandHandler#
	 * nodeImgCopyCommandFired(java.lang.String,
	 * neptune.web.client.view.components.NodesManager)
	 */
	public void nodeImgCopyCommandFired(String nodeId, NodesManager sender) {
		Command saveAsTemplateCommand = new SaveNodeAsTemplateCommand(nodeId
				+ EXPID_NODEID_SEPARATOR + expId);
		saveAsTemplateCommand.execute();
	}

}
